package delete;

import com.entity.Event;
import com.entity.Invitee;
import com.entity.User;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

@ManagedBean
@ViewScoped
public class ScheduleView implements Serializable
{

    @EJB
    private com.ejb.EventFacade ejbFacade;
    @EJB
    private com.ejb.UserFacade userEjbFacade;
    @PersistenceContext
    private EntityManager em;
    private ScheduleModel eventModel;

    private ScheduleEvent event = new DefaultScheduleEvent();

    @PostConstruct
    public void init()
    {
//	eventModel = new DefaultScheduleModel();
//        FacesContext context = FacesContext.getCurrentInstance();
//        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
//	List<Event> temp;
//	User u = userEjbFacade.find(request.getUserPrincipal().getName());
//	temp = em.createNamedQuery("Event.findByUserEmail", Event.class).setParameter("userEmail", u).getResultList();
//
//	for (int i = 0; i < temp.size(); i++)
//	{
//	    eventModel.addEvent(new DefaultScheduleEvent(temp.get(i).getEventName(), addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventStartTime()), addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventEndTime())));
//	}
    }

    public ScheduleModel getEventModel(User u)
    {
	eventModel = new DefaultScheduleModel();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
	List<Event> temp;
        List<Invitee> invitee;
	
	if(u == null){ 
            u = userEjbFacade.find(request.getUserPrincipal().getName());
            invitee = em.createNamedQuery("Invitee.findByUserEmailAccepted", Invitee.class).setParameter("userEmail", u).setParameter("accepted", true).getResultList();
            temp = em.createNamedQuery("Event.findByUserEmail", Event.class).setParameter("userEmail", u).getResultList();
            for (int i = 0; i < temp.size(); i++)
                {
                        eventModel.addEvent(new DefaultScheduleEvent(temp.get(i).getEventName(), addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventStartTime()), addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventEndTime())));
                }
            for (int i = 0; i < invitee.size(); i++)
            {   
                eventModel.addEvent(new DefaultScheduleEvent(invitee.get(i).getEventID().getEventName(), addDateAndTime(invitee.get(i).getEventID().getEventDate(), invitee.get(i).getEventID().getEventStartTime()), addDateAndTime(invitee.get(i).getEventID().getEventDate(), invitee.get(i).getEventID().getEventEndTime())));

            }
            return eventModel;
        }
        else{
            temp = em.createNamedQuery("Event.findByUserEmail", Event.class).setParameter("userEmail", u).getResultList();

            if (u.getUserCalendar())
            {
                for (int i = 0; i < temp.size(); i++)
                {
                    if(temp.get(i).getEventPublicPrivate().equals(true))
                        eventModel.addEvent(new DefaultScheduleEvent(temp.get(i).getEventName(), addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventStartTime()), addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventEndTime())));
                    else
                        eventModel.addEvent(new DefaultScheduleEvent("busy time slot", addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventStartTime()), addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventEndTime())));

                }
                return eventModel;
            }
            else
            {
                for (int i = 0; i < temp.size(); i++)
                {
                    eventModel.addEvent(new DefaultScheduleEvent("busy time slot", addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventStartTime()), addDateAndTime(temp.get(i).getEventDate(), temp.get(i).getEventEndTime())));

                }
            }
            return eventModel;
        }
    }

    private Date addDateAndTime(Date date, Date time)
    {
	return new Date(date.getYear(), date.getMonth(), date.getDate(), time.getHours(), time.getMinutes(), time.getSeconds());
    }

    public ScheduleEvent getEvent()
    {
	return event;
    }

    public void setEvent(ScheduleEvent event)
    {
	this.event = event;
    }

    public void addEvent(ActionEvent actionEvent)
    {
	if (event.getId() == null)
	{
	    eventModel.addEvent(event);
	} else
	{
	    eventModel.updateEvent(event);
	}

	event = new DefaultScheduleEvent();
    }

    public void onEventSelect(SelectEvent selectEvent)
    {
	event = (ScheduleEvent) selectEvent.getObject();
    }

    public void onDateSelect(SelectEvent selectEvent)
    {
	event = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());
    }

    public void onEventMove(ScheduleEntryMoveEvent event)
    {
	FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event moved", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());

	addMessage(message);
    }

    public void onEventResize(ScheduleEntryResizeEvent event)
    {
	FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event resized", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());

	addMessage(message);
    }

    private void addMessage(FacesMessage message)
    {
	FacesContext.getCurrentInstance().addMessage(null, message);
    }
}
